﻿using System;
using Microsoft.VisualBasic;
using GC2.Entities.Admin;

namespace GC2.Funciones.Encriptar
{    
    /// <summary>
    /// Descripción breve de Seguridad
    /// </summary>
    public class Seguridad
    {
        private static String Clave = "9517538264";
        private static String Delimitador = "-<=[HAA]=>-";

        public Seguridad()
        {
            //
            // TODO: Agregar aquí la lógica del constructor
            //
        }

        public Usuarios UsuarioIngreso(String P)
        {
            Usuarios usu = null;
            //Strings dat = DesEncriptar(P);
            String[] data = Strings.Split(DesEncriptar(P), Delimitador, -1, Microsoft.VisualBasic.CompareMethod.Text);
            
            if (data.Length == 3)
            {
                usu = new Usuarios();
                usu.Cliente = data[0];
                usu.Login = data[1];
                usu.PassWord = data[2];
            }
            
            return usu;
        }

        public String Encriptar(String S)
        {
            try
            {
                return S;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
        public String DesEncriptar(String S)
        {
            try
            {                
                String R = "";
                String r2 = "";
                int C1 = 0;
                int C2 = 0;
                int i = 0;

                for (int I = 0; I < S.Length; I++)
			    {
    			    r2 += Strings.Chr(Convert.ToInt16(S.Substring(I, 3)));
                    I++;
                    I++;
			    }    

                S = r2;

                if (Clave.Length > 0)
	            {
            		 for (int I = 0; I < S.Length; I++)
			        {
                        i = I + 1;
                        C1 = Strings.Asc(S.Substring(I, 1));
        			    if (i > Clave.Length)
	                    {
                            C2 = Strings.Asc(Clave.Substring((i % Clave.Length + 1) - 1, 1));
	                    }
                        else
	                    {
                            C2 = Strings.Asc(Clave.Substring(I, 1));                            
	                    }
                        C1 = C1 - C2 - 64;
                        if (System.Math.Sign(C1) == -1)
	                    {
                    		 C1 = 256 + C1;
	                    }
                        R += Strings.Chr(C1);
			        }
	            }
                else
	            {
                    R = S;
	            }
                
                return R;
            }
            catch (Exception ex)
            {
                //throw ex;
                String s = "";
                return s;
            }
        }
    }
}
